import math
import numpy as np
import matplotlib.pyplot as plt


a=3.5
b=4
error=0.0001
delta=0.0001

times=math.ceil(math.log((b-a)/error,2))

cnt=0

def fun(x):
    return 3*x*x-np.exp(x)

def d2(a,b):
    global cnt
    global times
    cnt+=1


    mid = (a + b) / 2
    if cnt==times or b-a<=delta:
        plt.scatter(mid,fun(mid),c='g')
        return mid
    elif fun(mid)<0:
        plt.scatter(mid, fun(mid), c='g')
        return d2(a,mid)
    elif fun(mid)>0:
        plt.scatter(mid, fun(mid), c='g')
        return d2(mid,b)
    else:
        plt.scatter(mid, fun(mid), c='g')
        return mid


#plt.style.use('seaborn-whitegrid')
plt.style.use('ggplot')

#绘制函数图和y=0处的红色虚线
x=np.linspace(a,b,1000)
y=fun(x)
plt.axhline(y=0, color='red', linestyle='--')
plt.plot(x,y)





print("%.3f"%d2(a,b))


plt.show()